{% extends 'custom_admin/base.html' %}

{% block title %}文章管理{% endblock %}

{% block content %}
<div class="page-heading">
    <h4><i class="fas fa-file-alt me-2"></i>文章管理</h4>
</div>

<div class="card mb-4">
    <div class="card-body">
        <div class="d-flex justify-content-between flex-wrap mb-3">
            <div class="mb-2">
                <a href="{% url 'custom_admin:article_create' %}" class="btn btn-primary btn-sm">
                    <i class="fas fa-plus me-1"></i>创建文章
                </a>
                <a href="{% url 'custom_admin:article_review_list' %}" class="btn btn-info btn-sm">
                    <i class="fas fa-check-circle me-1"></i>文章审核
                </a>
            </div>
            <div class="d-flex flex-wrap">
                <form class="d-flex" method="get">
                    <input type="text" name="title" class="form-control me-2" placeholder="按标题搜索" value="{{ title }}">
                    <select name="status" class="form-select me-2" style="width: initial;">
                        <option value="">全部状态</option>
                        <option value="draft" {% if status == 'draft' %}selected{% endif %}>草稿</option>
                        <option value="pending" {% if status == 'pending' %}selected{% endif %}>待审核</option>
                        <option value="first_approved" {% if status == 'first_approved' %}selected{% endif %}>一审通过</option>
                        <option value="approved" {% if status == 'approved' %}selected{% endif %}>审核通过</option>
                        <option value="rejected" {% if status == 'rejected' %}selected{% endif %}>审核拒绝</option>
                        <option value="published" {% if status == 'published' %}selected{% endif %}>已发布</option>
                        <option value="withdrawn" {% if status == 'withdrawn' %}selected{% endif %}>已撤回</option>
                    </select>
                    <button type="submit" class="btn btn-primary me-2 w2">筛选</button>
                    <a href="{% url 'custom_admin:article_list' %}" class="btn btn-outline-secondary w2">重置</a>
                </form>
            </div>
        </div>

        <div class="table-responsive">
            <table class="table table-striped">
                <thead>
                    <tr>
                        <th>标题</th>
                        <th>作者</th>
                        <th>状态</th>
                        <th>创建时间</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    {% for article in articles %}
                    <tr>
                        <td>{{ article.title }}</td>
                        <td>{{ article.author.username }}</td>
                        <td>
                            {% if article.status == 'draft' %}
                                <span class="badge bg-secondary">草稿</span>
                            {% elif article.status == 'pending' %}
                                <span class="badge bg-warning">待审核</span>
                            {% elif article.status == 'first_approved' %}
                                <span class="badge bg-info">一审通过</span>
                            {% elif article.status == 'approved' %}
                                <span class="badge bg-success">审核通过</span>
                            {% elif article.status == 'rejected' %}
                                <span class="badge bg-danger">审核拒绝</span>
                            {% elif article.status == 'published' %}
                                <span class="badge bg-primary">已发布</span>
                            {% elif article.status == 'withdrawn' %}
                                <span class="badge bg-dark">已撤回</span>
                            {% endif %}
                        </td>
                        <td>{{ article.created_at|date:"Y-m-d H:i" }}</td>
                        <td>
                            <div class="btn-group" role="group">
                                <a href="{% url 'custom_admin:article_edit' article.pk %}" class="btn btn-sm btn-outline-primary">
                                    <i class="fas fa-edit"></i> 编辑
                                </a>
                                {% if article.status == 'draft' %}
                                    <a href="{% url 'custom_admin:article_submit_for_review' article.pk %}" class="btn btn-sm btn-outline-warning">
                                        <i class="fas fa-paper-plane"></i> 提交审核
                                    </a>
                                {% elif article.status == 'approved' and not article.published %}
                                    <a href="{% url 'custom_admin:article_publish' article.pk %}" class="btn btn-sm btn-outline-success">
                                        <i class="fas fa-upload"></i> 发布
                                    </a>
                                {% elif article.status == 'published' %}
                                    <a href="{% url 'custom_admin:article_withdraw' article.pk %}" class="btn btn-sm btn-outline-secondary">
                                        <i class="fas fa-download"></i> 撤回
                                    </a>
                                {% endif %}
                                <a href="{% url 'custom_admin:article_delete' article.pk %}" class="btn btn-sm btn-outline-danger">
                                    <i class="fas fa-trash"></i> 删除
                                </a>
                            </div>
                        </td>
                    </tr>
                    {% empty %}
                    <tr>
                        <td colspan="5" class="text-center">暂无文章。</td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
        
        <!-- 分页控件 -->
        {% if articles.has_other_pages %}
        <div class="d-flex justify-content-between align-items-center mt-3">
            <div>
                <p class="mb-0">总共 {{ paginator.count }} 篇文章</p>
            </div>
            <div>
                <nav aria-label="文章分页">
                    <ul class="pagination mb-0">
                        {% if articles.has_previous %}
                            <li class="page-item">
                                <a class="page-link" href="?page=1{% if status %}&status={{ status }}{% endif %}{% if title %}&title={{ title }}{% endif %}&page_size={{ page_size }}">首页</a>
                            </li>
                            <li class="page-item">
                                <a class="page-link" href="?page={{ articles.previous_page_number }}{% if status %}&status={{ status }}{% endif %}{% if title %}&title={{ title }}{% endif %}&page_size={{ page_size }}">上一页</a>
                            </li>
                        {% else %}
                            <li class="page-item disabled">
                                <span class="page-link">首页</span>
                            </li>
                            <li class="page-item disabled">
                                <span class="page-link">上一页</span>
                            </li>
                        {% endif %}
                        
                        {% for num in articles.paginator.page_range %}
                            {% if articles.number == num %}
                                <li class="page-item active">
                                    <span class="page-link">{{ num }}</span>
                                </li>
                            {% elif num > articles.number|add:'-3' and num < articles.number|add:'3' %}
                                <li class="page-item">
                                    <a class="page-link" href="?page={{ num }}{% if status %}&status={{ status }}{% endif %}{% if title %}&title={{ title }}{% endif %}&page_size={{ page_size }}">{{ num }}</a>
                                </li>
                            {% endif %}
                        {% endfor %}
                        
                        {% if articles.has_next %}
                            <li class="page-item">
                                <a class="page-link" href="?page={{ articles.next_page_number }}{% if status %}&status={{ status }}{% endif %}{% if title %}&title={{ title }}{% endif %}&page_size={{ page_size }}">下一页</a>
                            </li>
                            <li class="page-item">
                                <a class="page-link" href="?page={{ paginator.num_pages }}{% if status %}&status={{ status }}{% endif %}{% if title %}&title={{ title }}{% endif %}&page_size={{ page_size }}">末页</a>
                            </li>
                        {% else %}
                            <li class="page-item disabled">
                                <span class="page-link">下一页</span>
                            </li>
                            <li class="page-item disabled">
                                <span class="page-link">末页</span>
                            </li>
                        {% endif %}
                    </ul>
                </nav>
            </div>
            <div>
                <form method="get" class="d-inline-block">
                    {% if status %}
                        <input type="hidden" name="status" value="{{ status }}">
                    {% endif %}
                    {% if title %}
                        <input type="hidden" name="title" value="{{ title }}">
                    {% endif %}
                    <input type="hidden" name="page_size" value="{{ page_size }}">
                    <div class="input-group input-group-sm" style="width: 150px;">
                        <input type="number" class="form-control" name="page" min="1" max="{{ paginator.num_pages }}" value="{{ articles.number }}">
                        <button class="btn btn-outline-secondary" type="submit">跳转</button>
                    </div>
                </form>
                <div class="d-inline-block ms-2">
                    <form method="get" class="d-inline-block">
                        {% if status %}
                            <input type="hidden" name="status" value="{{ status }}">
                        {% endif %}
                        {% if title %}
                            <input type="hidden" name="title" value="{{ title }}">
                        {% endif %}
                        <select name="page_size" class="form-select form-select-sm d-inline-block w-auto" onchange="this.form.submit()">
                            <option value="10" {% if page_size == 10 %}selected{% endif %}>10条/页</option>
                            <option value="20" {% if page_size == 20 %}selected{% endif %}>20条/页</option>
                            <option value="50" {% if page_size == 50 %}selected{% endif %}>50条/页</option>
                        </select>
                    </form>
                </div>
            </div>
        </div>
        {% endif %}
    </div>
</div>
{% endblock %}